package cn.sfrog.system.service.impl;

import cn.sfrog.common.utils.SecurityUtils;
import cn.sfrog.system.domain.SysAppVersion;
import cn.sfrog.system.domain.vo.BizStatusChangeVO;
import cn.sfrog.system.domain.vo.biz.SysAppVersionFormVO;
import cn.sfrog.system.domain.vo.biz.SysAppVersionQueryVO;
import cn.sfrog.system.domain.vo.biz.SysAppVersionVO;
import cn.sfrog.system.mapper.SysAppVersionMapper;
import cn.sfrog.system.service.ISysAppVersionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * APP版本信息Service业务层处理
 */
@Service
public class SysAppVersionServiceImpl extends ServiceImpl<SysAppVersionMapper, SysAppVersion> implements ISysAppVersionService {
    @Resource
    private SysAppVersionMapper sysAppVersionMapper;

    /**
     * 查询APP版本信息
     *
     * @param id APP版本信息主键
     * @return APP版本信息
     */
    @Override
    public SysAppVersionVO selectSysAppVersionById(Long id) {
        return sysAppVersionMapper.selectSysAppVersionById(id);
    }

    /**
     * 查询APP版本信息列表
     *
     * @param query APP版本信息
     * @return APP版本信息
     */
    @Override
    public List<SysAppVersionVO> selectSysAppVersionList(SysAppVersionQueryVO query) {
        return sysAppVersionMapper.selectSysAppVersionList(query);
    }

    /**
     * 新增APP版本信息
     *
     * @param form APP版本信息
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean insertSysAppVersion(SysAppVersionFormVO form) {
        SysAppVersion sysAppVersion = new SysAppVersion();
        BeanUtils.copyProperties(form, sysAppVersion);
        sysAppVersion.setCreateTime(new Date());
        sysAppVersion.setCreateBy(SecurityUtils.getUsername());
        sysAppVersion.setUpdateTime(new Date());
        sysAppVersion.setUpdateBy(SecurityUtils.getUsername());
        return sysAppVersion.insert();
    }

    /**
     * 修改APP版本信息
     *
     * @param form APP版本信息
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean updateSysAppVersion(SysAppVersionFormVO form) {
        SysAppVersion sysAppVersion = new SysAppVersion();
        BeanUtils.copyProperties(form, sysAppVersion);
        sysAppVersion.setUpdateTime(new Date());
        sysAppVersion.setUpdateBy(SecurityUtils.getUsername());
        return sysAppVersion.updateById();
    }

    /**
     * 批量删除APP版本信息
     *
     * @param ids 需要删除的APP版本信息主键
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean deleteSysAppVersionByIds(Long[] ids) {
            return SysAppVersion.DAO.delete(Wrappers.lambdaQuery(SysAppVersion.class).in(SysAppVersion::getId, (Object[]) ids));
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean changeStatus(BizStatusChangeVO vo) {
            return SysAppVersion.DAO.update(Wrappers.lambdaUpdate(SysAppVersion.class).in(SysAppVersion::getId, (Object[]) vo.getIds()).set(SysAppVersion::getStatus, vo.getStatus()));
    }

    @Override
    public SysAppVersion selectAppLatestVersion(String type) {
        LambdaQueryWrapper<SysAppVersion> lambdaQueryWrapper = new LambdaQueryWrapper<SysAppVersion>()
                .eq(SysAppVersion::getType,type)
                .orderByDesc(SysAppVersion::getReleaseTime);
        SysAppVersion version = SysAppVersion.DAO.selectOne(lambdaQueryWrapper);
        return version;
    }
}
